package cn.intcast.order.adapter;

import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;


import com.bumptech.glide.Glide;

import java.util.List;

import cn.intcast.order.R;
import cn.intcast.order.actvity.ShopDetailActivity;
import cn.intcast.order.bean.ShopBean;

public class ShopAdapter extends BaseAdapter {
    private Context mContext;
    private List<ShopBean> sbl;

    public ShopAdapter(Context mContext) {
        this.mContext = mContext;
    }

    public void setData(List<ShopBean> sbl){
        this.sbl=sbl;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
            return sbl==null?0:sbl.size();
    }

    @Override
    public ShopBean getItem(int i) {
        return sbl==null?null:sbl.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        final ViewHolder vh;
        if(view ==null){
            vh=new ViewHolder();
            view= LayoutInflater.from(mContext).inflate(R.layout.shop_item,null);
            vh.tv_shop_name=view.findViewById(R.id.tv_shop_name);
            vh.tv_sale_num=view.findViewById(R.id.tv_sale_num);
            vh.tv_cost=view.findViewById(R.id.tv_cost);
            vh.tv_welfate=view.findViewById(R.id.tv_welfate);
            vh.tv_time=view.findViewById(R.id.tv_time);
            vh.iv_shop_pic=view.findViewById(R.id.iv_shop_pic);
            view.setTag(vh);
        }else{
            vh= (ViewHolder) view.getTag();
        }
        final ShopBean bean = getItem(i);
        if(bean!=null){
            vh.tv_shop_name.setText(bean.getShopName());
            vh.tv_sale_num.setText("月售"+bean.getSaleNum());
            vh.tv_cost.setText("起送￥"+bean.getOfferPrice()+"|配送￥"+bean.getDistributionCost());
            vh.tv_time.setText(bean.getTime());
            vh.tv_welfate.setText(bean.getWelfare());
            Glide.with(this.mContext)
                    .load(bean.getShopPic())
                    .error(R.mipmap.ic_launcher)
                    .into(vh.iv_shop_pic);
        }
        view.setOnClickListener(view1 -> {
            if(bean==null)return;
            Intent intent = new Intent(mContext, ShopDetailActivity.class);
            intent.putExtra("shop",bean);
            Log.e("TAG", "getView: "+bean);
            mContext.startActivity(intent);
        });

        return view;
    }

    class ViewHolder{
        public TextView tv_shop_name,tv_sale_num,tv_cost,tv_welfate,tv_time;
        public ImageView iv_shop_pic;
    }
}
